home *** CD-ROM | disk | FTP | other *** search
- #include <stdlib.h>
- #include <stdio.h>
-
- #define MAX_ARTIST_CHARS 50
- #define MAX_TITLE_CHARS 50
-
- struct CDInfo
- {
- char rating;
- char artist[ MAX_ARTIST_CHARS ];
- char title[ MAX_TITLE_CHARS ];
- struct CDInfo *next;
- } *gFirstPtr, *gLastPtr;
-
-
- /******************************** Flush ***/
-
- void Flush()
- {
- while ( getchar() != '\n' )
- ;
- }
-
-
- /******************************** ReadLine ***/
-
- void ReadLine( char *line )
- {
- char c;
-
- while ( (c = getchar()) != '\n' )
- {
- *line = c;
- line++;
- }
-
- *line = 0;
- }
-
-
- /******************************** ListCDs ***/
-
- void ListCDs()
- {
- struct CDInfo *curPtr;
-
- if ( gFirstPtr == NULL )
- {
- printf( "No CDs have been entered yet...\n" );
- printf( "\n----------\n" );
- }
- else
- {
- curPtr = gFirstPtr;
-
- while ( curPtr != NULL )
- {
- printf( "Artist: %s\n", curPtr->artist );
- printf( "Title: %s\n", curPtr->title );
- printf( "Rating: %d\n", curPtr->rating );
-
- printf( "\n----------\n" );
-
- curPtr = curPtr->next;
- }
- }
- }
-
-
- /******************************** AddToList ***/
-
- void AddToList( struct CDInfo *curPtr )
- {
- if ( gFirstPtr == NULL )
- gFirstPtr = curPtr;
- else
- gLastPtr->next = curPtr;
-
- gLastPtr = curPtr;
- curPtr->next = NULL;
- }
-
-
- /******************************** ReadStruct ***/
-
- struct CDInfo *ReadStruct()
- {
- struct CDInfo *infoPtr;
- int num;
-
- infoPtr = malloc( sizeof( struct CDInfo ) );
-
- if ( infoPtr == NULL )
- {
- printf( "Out of memory!!! Goodbye!\n" );
- exit( 0 );
- }
-
- printf( "Enter Artist's Name: " );
- ReadLine( infoPtr->artist );
-
- printf( "Enter CD Title: " );
- ReadLine( infoPtr->title );
-
- num = 0;
- while ( ( num < 1 ) || ( num > 10 ) )
- {
- printf( "Enter CD Rating (1-10): " );
- scanf( "%d", &num );
- Flush();
- }
-
- infoPtr->rating = num;
-
- printf( "\n----------\n" );
-
- return( infoPtr );
- }
-
-
- /******************************** GetCommand ***/
-
- char GetCommand()
- {
- char command = 0;
-
- while ( (command != 'q') && (command != 'n')
- && (command != 'l') )
- {
- printf( "Enter command (q=quit, n=new, l=list): " );
- scanf( "%c", &command );
- Flush();
- }
-
- printf( "\n----------\n" );
- return( command );
- }
-
-
- /******************************** main ***/
-
- main()
- {
- char command;
-
- gFirstPtr = NULL;
- gLastPtr = NULL;
-
- while ( (command = GetCommand() ) != 'q' )
- {
- switch( command )
- {
- case 'n':
- AddToList( ReadStruct() );
- break;
- case 'l':
- ListCDs();
- break;
- }
- }
-
- printf( "Goodbye..." );
- }